import store from "../store";
//白名单 不需要登录的页面路径组成的数组
const whiteList = [
	'/pages/subpage/list/service',
	'/pages/subpage/list/location',
	'/pages/subpage/list/serv_detail',
	'/pages/subpage/list/dem_detail',
	'/pages/subpage/search/index',
	'/pages/subpage/login/oneBtnLogin',
	'/pages/subpage/login/login',
	'/pages/subpage/about/introduction',
	'/pages/subpage/about/rule-file',
	'/pages/subpage/about/service',
	'/pages/subpage/about/mpShare',
	'/pages/subpage/webview/webview',
	'/pages/subpage/about/entry',
	'/pages/subpage/profile/index'
];

function hasPermission(url) {
	// 在白名单中或有token，直接跳转
	if (whiteList.indexOf(url) !== -1 || store.state.vuex_token) {
		return true;
	}
	return false;
}

uni.addInterceptor("navigateTo", {
	// 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
	invoke(e) {
		const url = e.url.split('?')[0]
		if (!hasPermission(url)) {
			// #ifdef H5
			uni.navigateTo({
				url: "/pages/subpage/login/login",
			});
			// #endif
			// #ifndef H5
			uni.navigateTo({
				url: "/pages/subpage/login/oneBtnLogin",
			});
			// #endif

			return false;
		}
		return true;
	},
	success(e) {
		// console.log(e)
	},
	fail(err) {
		console.log(err);
	},
	complete(res) {
		// console.log(res);
	}
});